﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
	/**
	 * Classname : Quyen
	 * Version information :1.0
	 * Date  :26-09-2006
	 * Copyright notice : 
	 */
	
	public class DAQuyen : DbObject
	{
		protected SqlConnection connec;

        public DAQuyen(SqlConnection Connection)
		{
            connec = Connection;
		}
        //loadGanQuyen

        public DataSet loadGanQuyenByQuyenID(int QuyenID)
        {
            // create the parameter
            SqlParameter[] parameters = { new SqlParameter("@QuyenID", SqlDbType.Int, 4) };
            parameters[0].Value = QuyenID;

            return RunProcedure("sp_GanQuyen_loadGanQuyenByQuyenID", parameters, "TB_GanQuyen", connec);
        }
        //Quyen Level
        public DataSet loadQuyenLevel(int QuyenID)
        {
            // create the parameter
            SqlParameter[] parameters = { new SqlParameter("@QuyenID", SqlDbType.Int, 4) };
            parameters[0].Value = QuyenID;

            return RunProcedure("sp_QuyenLevel_loadQuyenLevelByQuyen", parameters, "TB_QuyenLevel", connec);
        }
        //Quyen chuc nang
        public DataSet loadQuyenChucnang(int QuyenID)
        {
            // create the parameter
            SqlParameter[] parameters = { new SqlParameter("@QuyenID", SqlDbType.Int, 4) };
            parameters[0].Value = QuyenID;

            return RunProcedure("sp_QuyenChucnang_loadQuyenChucnangByQuyen", parameters, "TB_QuyenChucnang", connec);
        }
        public DataSet loadQuyenGridView()
        {
            DataSet dsQuyen = RunProcedure("sp_Quyen_loadQuyenGridView", "Tb_Quyen", connec);
            return dsQuyen;
        }
		// return all the Quyen select Status
		// Danh cho nguoi su dung
		public DataSet loadQuyenbyParent(int ID)
		{
			// create the parameter
			SqlParameter[] parameters = { new SqlParameter("@ID", SqlDbType.Int, 4) };
			parameters[0].Value = ID;

            return RunProcedure("sp_Quyen_loadQuyenbyParent", parameters, "Tb_Quyen", connec);
		}
        // return all the Quyen select Language
		// Danh cho nguoi quan tri
		public DataSet loadQuyen()
		{
            DataSet dsQuyen = RunProcedure("sp_Quyen_loadQuyen", "Tb_Quyen", connec);
            for (int i = 0; i < dsQuyen.Tables[0].Rows.Count; i++)
            {
                dsQuyen.Tables[0].Rows[i]["Seq"] = (i + 1).ToString();
            }
            return dsQuyen;
		}
		
		// return only the record with the specified ID!!!!!!!!!!!
		public DataRow loadID(int ID)
		{
			// create the parameter
			SqlParameter[] parameters = { new SqlParameter("@ID", SqlDbType.Int, 4) };
			parameters[0].Value = ID;

            using (DataSet Quyen = RunProcedure("sp_Quyen_loadID", parameters, "Tb_Quyen", connec))
			{
				return Quyen.Tables[0].Rows[0];
			}
		}	
		// update the properties of the specified Quyen
		// Sua noi dung ban ghi hien hanh
		public int updateQuyen(int ID,string Tennhom,int NhomID)
		{
			int numAffected;
			// create the parameters
			SqlParameter[] parameters = { 	new SqlParameter("@ID", SqlDbType.Int, 4),
											new SqlParameter("@Tennhom", SqlDbType.NVarChar, 256),	
											new SqlParameter("@NhomID", SqlDbType.Int ,4)
										};
			// set the values
			parameters[0].Value = ID;
			parameters[1].Value = Tennhom.Trim();
			parameters[2].Value = NhomID;		
			// run the procedure
            RunProcedure("sp_Quyen_updateQuyen", parameters, out numAffected, connec);

			return numAffected;
		}

		// add a new question
		// Them ban ghi hien hanh
		public int insertQuyen(string Tennhom,int NhomID)
		{
			int numAffected;
			// create the parameters
			SqlParameter[] parameters =  {	 new SqlParameter("@Tennhom", SqlDbType.NVarChar, 256), 
											 new SqlParameter("@NhomID", SqlDbType.Int ,4),
											 new SqlParameter("@ID", SqlDbType.Int ,4)
										 };	
			// set the values
			parameters[0].Value = Tennhom.Trim();
			parameters[1].Value = NhomID;
			parameters[2].Direction = ParameterDirection.Output;
		
			// run the procedure
            RunProcedure("sp_Quyen_insertQuyen", parameters, out numAffected, connec);

			return (int)parameters[2].Value;
		}
		// Xoa Role
		public int removeQuyen(int ID)
		{
			int numAffected;
			// create the parameters
			SqlParameter[] parameters = {	new SqlParameter("@ID", SqlDbType.Int)	};
			parameters[0].Value = ID;			
			// run the procedure
            RunProcedure("sp_Quyen_removeQuyen", parameters, out numAffected, connec);
			return numAffected;			
		}
        //Quyen Level 
        //Insert        
        public int InsertQuyenLevel(int LevelID,int QuyenID,bool Doc,bool Ghi,bool Sua,bool Xoa)
        {
            int numAffected;
            // create the parameters
            SqlParameter[] parameters = { 	new SqlParameter("@LevelID", SqlDbType.Int, 4),
											new SqlParameter("@QuyenID", SqlDbType.Int, 4),	
											new SqlParameter("@Doc", SqlDbType.Bit ,1),
                                            new SqlParameter("@Ghi", SqlDbType.Bit ,1),
                                            new SqlParameter("@Sua", SqlDbType.Bit ,1),
                                            new SqlParameter("@Xoa", SqlDbType.Bit ,1)
										};
          // set the values
			parameters[0].Value = LevelID;
			parameters[1].Value = QuyenID;
		    parameters[2].Value = Doc;
		    parameters[3].Value = Ghi;
		    parameters[4].Value = Sua;
            parameters[5].Value = Xoa;			
            // run the procedure
            RunProcedure("SP_QuyenLevel_CreateQuyenLevel", parameters, out numAffected, connec);
            return numAffected;
        }
        //remove
        public int removeQuyenLevel(int QuyenID)
        {
            int numAffected;
            // create the parameters
            SqlParameter[] parameters = { new SqlParameter("@QuyenID", SqlDbType.Int)
                                        };
            parameters[0].Value = QuyenID;           
            // run the procedure
            RunProcedure("sp_QuyenLevel_removeQuyenLevel", parameters, out numAffected, connec);
            return numAffected;
        }

        //Quyen Chuc Nang
        //Insert        
        public int InsertQuyenChucNang(int QuyenID, int ChucNangID)
        {
            int numAffected;
            // create the parameters
            SqlParameter[] parameters = { 	new SqlParameter("@QuyenID", SqlDbType.Int, 4),
											new SqlParameter("@ChucnangID", SqlDbType.Int, 4)	
											
										};
            // set the values
            parameters[0].Value = QuyenID;
            parameters[1].Value = ChucNangID;          
            // run the procedure
            RunProcedure("SP_QuyenChucNang_CreateQuyenChucNang", parameters, out numAffected, connec);
            return numAffected;
        }
        //remove
        public int removeQuyenChucNang(int QuyenID)
        {
            int numAffected;
            // create the parameters
            SqlParameter[] parameters = { new SqlParameter("@QuyenID", SqlDbType.Int)
                                        };
            parameters[0].Value = QuyenID;
            // run the procedure
            RunProcedure("sp_QuyenChucNang_removeQuyenChucNang", parameters, out numAffected, connec);
            return numAffected;
        }

        /// <summary>
        /// PhiNV
        /// Hàm check Quyền
        /// </summary>
        /// <param name="QuyenID"></param>
        /// <param name="ChucNangID"></param>
        /// <returns>bool</returns>
        public bool CheckQuyen(int UserID, int ChucNangID)
        {
            try
            {
                // create the parameters
                SqlParameter[] parameters = { 	new SqlParameter("@UserID", SqlDbType.Int, 4),
											new SqlParameter("@ChucnangID", SqlDbType.Int, 4)	
											
										};
                // set the values
                parameters[0].Value = UserID;
                parameters[1].Value = ChucNangID;
                using (DataSet dsLevel = RunProcedure("sp_Quyen_GetQuyenByUser", parameters, "TB_ChucNang", connec))
                {
                    if (dsLevel.Tables[0].Rows.Count > 0)
                    {
                        return true;
                    }
                    else return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
            
        }
	}
}